
import datetime

def compose_meta(*to_compose):
    class ComposedMeta(type):
        pass
    for cfunc, args in to_compose:
        attr, insert_func = cfunc(ComposedMeta, *args)
        setattr(ComposedMeta, attr, insert_func)
    return ComposedMeta

def meta_append_to_list(compose, lst):
    def closure_init(cls, name, bases, dct):
        super(compose, cls).__init__(name, bases, dct)
        if hasattr(cls, "_ignore") and cls.__name__ in cls._ignore:
            return
        lst.append(cls)
    return '__init__', closure_init

def meta_name_attributes(compose, filter_fn, annotate_fn):
    def closure_new(cls, name, bases, attrs):
        new_instance = super(compose, cls).__new__(cls, name, bases, attrs)
        attr_val = zip(attrs, map(lambda a: getattr(new_instance, a), 
                                  attrs))
        for attr, val in filter(lambda v: filter_fn(*v), attr_val):
            annotate_fn(attr, val)
        return new_instance
    return '__new__', staticmethod(closure_new)

date_epoch = datetime.datetime(1970, 1, 1)
def date_to_int(dt):
    return (dt - date_epoch).days

def int_to_date(id):
    return date_epoch + datetime.timedelta(days=id)
    
# vim: expandtab:ts=4:sw=4:
